require "Util.TableUtil"

module("TableUtilTest", package.seeall, lunit.testcase)

function test_clone()
	local object = { true, 1, 0.1, "string", { true, 1, 0.1, "string" } }
	table.insert(object, object)

	local shadowClone = TableUtil.clone(object)

	assert_not_equal(object, shadowClone)

	assert_equal(object[5], shadowClone[5])
	assert_equal(object[6], shadowClone[6])

	local deepClone = TableUtil.clone(object, true)

	assert_not_equal(object, deepClone)

	assert_not_equal(object[5], deepClone[5])
	assert_not_equal(object[6], deepClone[6])

	assert_equal(deepClone, deepClone[6])
end

function test_print()
	local object = { true, 1, 0.1, "string", { true, 1, 0.1, "string" }, object }
	table.insert(object, object)

	print("\nDump table content:")
	TableUtil.print(object, print)
end

function test_pairsByKey()
	local object = { ["1"] = 3, ["2"] = 2, ["3"] = 1 }

	local sorter = function(key1, key2)
		return tonumber(key1) < tonumber(key2)
	end

	print("\nPairs by key:")
	for key, value in TableUtil.pairsByKey(object, sorter) do
		print(key, value)
	end
end

function test_pairsByValue()
	local object = { ["1"] = 3, ["2"] = 2, ["3"] = 1 }

	local sorter = function(value1, value2)
		return tonumber(value1) < tonumber(value2)
	end

	print("\nPairs by value:")
	for key, value in TableUtil.pairsByValue(object, sorter) do
		print(key, value)
	end
end
